## Allee and Scalera 2012 IO

library(foreign) 
library(Amelia)

## Load original dataset
as2012 <- read.dta("AS2012 IO Rep Data.dta")
head(as2012)
dim(as2012)

## Drop ID vars
as2012$country <- as2012$iso_code <- as2012$cowcode <-   as2012$ifscode <- NULL

## How many variables? 91: no reduction necessary
dim(as2012)

## Imputation
## What is average percentage of missing data?
NAs <- function(x) {
    as.vector(apply(x, 2, function(x) length(which(is.na(x)))))
    }
NAs(as2012)
mean(NAs(as2012)/nrow(as2012))*100

## Thus: 5 imputations

set.seed(02138)
as2012.out <- amelia(as2012, m = 5, ts = "year", cs = "uncode", polytime = 3, lags = c("lnFtrade", "member", "rigorous", "earlymem", "memce", "memre", "memrc", "colonial", "rigorouscounter", "colonialcounter"), empri = 0.01*nrow(as2012))

write.amelia(obj=as2012.out, file.stem = "AS2012 IO Imp Data", format = "dta", separate = FALSE)